package org.dromara.nursing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nursing.domain.vo.BedInfoVo;
import org.dromara.nursing.domain.bo.BedInfoBo;
import org.dromara.nursing.service.IBedInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 床位管理
 *
 * @author Xue
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nursing/bedInfo")
public class BedInfoController extends BaseController {

    private final IBedInfoService bedInfoService;
    private static final Logger log = LoggerFactory.getLogger(BedInfoController.class);

    /**
     * 查询床位管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<BedInfoVo> list(BedInfoBo bo, PageQuery pageQuery) {
        return bedInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出床位管理列表
     */
    @Log(title = "床位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BedInfoBo bo, HttpServletResponse response) {
        List<BedInfoVo> list = bedInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "床位管理", BedInfoVo.class, response);
    }

    /**
     * 获取床位管理详细信息
     */
    @GetMapping("/{bedId}")
    public R<BedInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bedId) {
        return R.ok(bedInfoService.queryById(bedId));
    }

    /**
     * 新增床位管理
     */
    @Log(title = "床位管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BedInfoBo bo) {
        return toAjax(bedInfoService.insertByBo(bo));
    }

    /**
     * 批量新增床位管理
     */
    @Log(title = "床位管理-批量新增", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public R<Void> batchAdd(@RequestBody List<BedInfoBo> boList) {
        try {
            boolean success = true;
            for (BedInfoBo bo : boList) {
                success = success && bedInfoService.insertByBo(bo);
            }
            return toAjax(success);
        } catch (Exception e) {
            log.error("批量添加床位失败", e);
            return R.fail("批量添加床位失败: " + e.getMessage());
        }
    }

    /**
     * 修改床位管理
     */
    @Log(title = "床位管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BedInfoBo bo) {
        return toAjax(bedInfoService.updateByBo(bo));
    }

    /**
     * 删除床位管理
     *
     * @param bedIds 主键串
     */
    @Log(title = "床位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bedIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bedIds) {
        try {
            log.info("删除床位请求，bedIds: {}", (Object) bedIds);
            boolean result = bedInfoService.deleteWithValidByIds(List.of(bedIds), true);
            if (result) {
                return R.ok();
            } else {
                return R.fail("删除失败，可能床位已被占用或不存在");
            }
        } catch (Exception e) {
            log.error("删除床位异常", e);
            return R.fail("删除床位异常: " + e.getMessage());
        }
    }
}
